const app = getApp();
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
const {
  $Toast
} = require('../../components/base/base');
var interval = null;

Page({

  data: {
    navBg: app.globalData.navBg,
    navColor: app.globalData.navColor,
    windowHeight: app.globalData.windowHeight - app.globalData.navHeight,
    navTop: app.globalData.navHeight,
    back: true,
    navBox: true,
    logoSrc: 'https://chattest.metromall.cn/wfm/images/login-logo.png', //登录界面logo
    isShowPassword: true,
    userName: '',
    mobile: '',
    smsCode: '',
    passWord: '',
    userNameClose: false,
    mobileClose: false,
    smsCodeClose: false,
    passWordClose: false,
    smsTime: '获取验证码', //按钮文字
    currentTime: 60, //倒计时
    disabled: false, //按钮是否禁用
    detain: true
  },

  //按钮禁用
  detainButton: function () {
    let { userName, mobile, smsCode, passWord } = this.data
    if (userName && mobile && smsCode && passWord) {
      this.setData({
        detain: false
      })
    } else {
      this.setData({
        detain: true
      })
    }

  },

  //监听用户名
  UserNameInput: function (e) {
    var userName = e.detail.value
    this.setData({
      userName: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (userName == null || userName == undefined || userName.length == 0 || userName == '') {
      this.setData({
        userNameClose: false
      })
    } else {
      this.setData({
        userNameClose: true
      })
    }
    this.detainButton()
  },

  UserNameFocus: function (e) {
    var userName = e.detail.value
    this.setData({
      userName: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (userName == null || userName == undefined || userName.length == 0 || userName == '') {
      this.setData({
        userNameClose: false
      })
    } else {
      this.setData({
        userNameClose: true
      })
    }
  },

  UserNameBlur: function (e) {
    this.setData({
      userNameClose: false
    })
  },

  CloseUNInput: function (e) {
    var userName = e.detail.value
    this.setData({
      userName: '',
      userNameClose: false
    });
    this.detainButton()
  },

  //监听手机号
  MobileInput: function (e) {
    var mobile = e.detail.value
    this.setData({
      mobile: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (mobile == null || mobile == undefined || mobile.length == 0 || mobile == '') {
      this.setData({
        mobileClose: false
      })
    } else {
      this.setData({
        mobileClose: true
      })
    }
    this.detainButton()
  },

  MobileFocus: function (e) {
    var mobile = e.detail.value
    this.setData({
      mobile: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (mobile == null || mobile == undefined || mobile.length == 0 || mobile == '') {
      this.setData({
        mobileClose: false
      })
    } else {
      this.setData({
        mobileClose: true
      })
    }
  },

  MobileBlur: function (e) {
    this.setData({
      mobileClose: false
    })
  },

  CloseMInput: function (e) {
    var mobile = e.detail.value
    this.setData({
      mobile: '',
      mobileClose: false
    });
    this.detainButton()
  },


  //监听短信验证码
  SmsCodeInput: function (e) {
    var smsCode = e.detail.value
    this.setData({
      smsCode: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (smsCode == null || smsCode == undefined || smsCode.length == 0 || smsCode == '') {
      this.setData({
        smsCodeClose: false
      })
    } else {
      this.setData({
        smsCodeClose: true
      })
    }
    this.detainButton()
  },

  SmsCodeFocus: function (e) {
    var smsCode = e.detail.value
    this.setData({
      smsCode: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (smsCode == null || smsCode == undefined || smsCode.length == 0 || smsCode == '') {
      this.setData({
        smsCodeClose: false
      })
    } else {
      this.setData({
        smsCodeClose: true
      })
    }
  },

  SmsCodeBlur: function (e) {
    this.setData({
      smsCodeClose: false
    })
  },

  CloseSCInput: function (e) {
    var smsCode = e.detail.value
    this.setData({
      smsCode: '',
      smsCodeClose: false
    });
    this.detainButton()
  },

  //监听登录密码
  PassWordInput: function (e) {
    var passWord = e.detail.value
    this.setData({
      passWord: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (passWord == null || passWord == undefined || passWord.length == 0 || passWord == '') {
      this.setData({
        passWordClose: false
      })
    } else {
      this.setData({
        passWordClose: true
      })
    }
    this.detainButton()
  },

  PassWordFocus: function (e) {
    var passWord = e.detail.value
    this.setData({
      passWord: e.detail.value.replace(/^\s+|\s+$/g, '')
    })
    if (passWord == null || passWord == undefined || passWord.length == 0 || passWord == '') {
      this.setData({
        passWordClose: false
      })
    } else {
      this.setData({
        passWordClose: true
      })
    }
  },

  PassWordBlur: function (e) {
    this.setData({
      passWordClose: false
    })
  },

  ClosePWInput: function (e) {
    var passWord = e.detail.value
    this.setData({
      passWord: '',
      passWordClose: false
    });
    this.detainButton()
  },


  //注册密码
  Register: util.throttle(function (e) {

    var that = this;
    var userName = that.data.userName;
    var mobile = that.data.mobile;
    var smsCode = that.data.smsCode;
    var passWord = that.data.passWord;
    if (userName == "" || userName == undefined || userName == null || userName.length == 0) {
      $Toast({
        content: '请输入用户名!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (userName.length < 4 || userName.length > 16) {
      $Toast({
        content: '用户名长度在4~16位之间,请重新输入!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (mobile == "" || mobile == undefined || mobile == null || mobile.length == 0) {
      $Toast({
        content: '请输入手机号!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (mobile.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(mobile)) {
      $Toast({
        content: '手机号格式不正确!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (smsCode == "" || smsCode == undefined || smsCode == null || smsCode.length == 0) {
      $Toast({
        content: '请输入短信验证码!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (smsCode.length < 4 || smsCode.length > 6) {
      $Toast({
        content: '短信验证码错误!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (passWord == "" || passWord == undefined || passWord == null || passWord.length == 0) {
      $Toast({
        content: '请输入登录密码!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else if (passWord.length < 6 || passWord.length > 18) {
      $Toast({
        content: '密码长度在6~18位之间,请重新输入!',
        icon: 'prompt_fill',
        duration: 1
      });
      return;
    } else {

      wx.request({
        url: util.api + api.Regist,
        data: {
          BUSINESS_PARAMETERS: {
            Mobile: that.data.mobile,
            smscode: that.data.smscode,
            password: that.data.password,
            memcardid: 100
          },
          SYSTEM_PARAMETERS: {
            APP_ID: "M_P_G001",
            METHOD_ID: 0,
            ACCESS_TOKEN: "1w2Re3bE^og38",
            TIMESTAMP: util.formatTime(new Date(), 'Y/M/D h:m:s'),
            SIGN: "string"
          }
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          //当手机号正确的时候提示用户短信验证码已经发送
          $Toast({
            content: "注册成功",
            icon: 'success',
            duration: 1
          })
          util.navigateTo('../login/login')
        }
      })
    }

  }, 1000),

  //短信验证码倒计时函数
  getSmsCode: function (options) {
    var that = this;
    that.setData({
      disabled: true, //只要点击了按钮就让按钮禁用 (避免正常情况下多次触发定时器事件)
      color: '#DEDEDE',
    })

    var mobile = that.data.mobile;
    var currentTime = that.data.currentTime //把手机号跟倒计时值变例成js值
    var warn = null; //warn为当手机号为空或格式不正确时提示用户的文字，默认为空
    if (mobile == "" || mobile == undefined || mobile == null || mobile.length == 0) {
      warn = "请输入手机号";
    } else if (mobile.trim().length != 11 || !/^1[3|4|5|6|7|8|9]\d{9}$/.test(mobile)) {
      warn = "手机号格式不正确";
    } else {
      wx.request({
        url: util.api + api.SendSMSCode,
        data: {
          BUSINESS_PARAMETERS: {
            msgType: 62,
            mobile: that.data.mobile
          },
          SYSTEM_PARAMETERS: {
            APP_ID: "M_P_G001",
            METHOD_ID: 0,
            ACCESS_TOKEN: "1w2Re3bE^og38",
            TIMESTAMP: util.formatTime(new Date(), 'Y/M/D h:m:s'),
            SIGN: "string"
          }
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          //当手机号正确的时候提示用户短信验证码已经发送
          $Toast({
            content: "短信验证码已发送",
            icon: 'prompt_fill',
            duration: 1
          })

          //设置一分钟的倒计时

          var interval = setInterval(function () {
            currentTime--; //每执行一次让倒计时秒数减一
            that.setData({
              smsTime: currentTime + 1 + 's', //按钮文字变成倒计时对应秒数
            })
            //如果当秒数小于等于0时 停止计时器 且按钮文字变成重新发送 且按钮变成可用状态 倒计时的秒数也要恢复成默认秒数 即让获取验证码的按钮恢复到初始化状态只改变按钮文字
            if (currentTime <= 0) {
              clearInterval(interval)
              that.setData({
                smsTime: '重新发送',
                currentTime: 60,
                disabled: false,
                color: '#DEDEDE'
              })
            }

          }, 1000);
        }
      })
    };

    //判断 当提示错误信息文字不为空 即手机号输入有问题时提示用户错误信息 并且提示完之后一定要让按钮为可用状态 因为点击按钮时设置了只要点击了按钮就让按钮禁用的情况
    if (warn != null) {
      $Toast({
        content: warn,
        icon: 'prompt_fill',
        duration: 1
      })
      that.setData({
        disabled: false,
        color: '#DEDEDE'
      })
      return;
    };

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  }
  

})